﻿using System.Collections.Generic;
using System.Globalization;

namespace MoBaoPay.Model.Request
{
    /// <summary>
    /// 退款参数
    /// </summary>
    internal class MoBaoRefundRequest : IMoBaoRequest
    {
        /// <summary>
        /// 接口名称
        /// </summary>
        public string ApiName { get; set; } = "REFUND";

        /// <summary>
        /// 商户号
        /// </summary>
        public string CustNo { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string MerchTradeNo { get; set; }
        
        /// <summary>
        /// 支付订单号
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 退款金额
        /// </summary>
        public decimal TradeAmt { get; set; }

        /// <summary>
        /// 交易IP
        /// </summary>
        public string TerminalId { get; set; }

        /// <summary>
        /// 签名
        /// </summary>
        public string SignMsg { get; set; }

        /// <summary>
        /// 请求地址
        /// </summary>
        private string requestUrl = "https://trade2.mobaopay.com/cashier/api/gateway.cgi";

        public string GetRequestUrl()
        {
            return requestUrl;
        }

        public void SetRequestUrl(string url)
        {
            requestUrl = url;
        }

        public IDictionary<string, string> GetRequestContent()
        {
            return new Dictionary<string, string>
            {
                { "apiName", ApiName },
                { "custNo", CustNo },
                { "merchTradeNo", MerchTradeNo },
                { "orderNo", OrderNo },
                { "tradeAmt", TradeAmt.ToString(CultureInfo.InvariantCulture) },
                { "terminalId", TerminalId },
                { "signMsg", SignMsg }
            };
        }

        public void Sign(string path, string password)
        {
            var strData =
                $"apiName={ApiName}&custNo={CustNo}&merchTradeNo={MerchTradeNo}&orderNo={OrderNo}&terminalId={TerminalId}&tradeAmt={TradeAmt}";
            this.SignMsg = MD5WithRSA.SignData(path, password, strData);
        }
    }
}